<?php

	class Player extends Model{

		private $_PlayerId;
		private $_Name;
		private $_Surname;
		private $_CountryId;
		private $_Birthdate;
		private $_Points;
		private $_Ranking;
		private $_Active;
		private $_Sex;

		/**
	 * @return the $_Sex
	 */
	public function getSex() {
		return $this->_Sex;
	}

		/**
	 * @param field_type $_Sex
	 */
	public function setSex($_Sex) {
		$this->_Sex = $_Sex;
	}

		function __construct($arrInitialValues = null){
			parent::__construct();

			if (isset($arrInitialValues['player_id']))
				$this->setPlayerId($arrInitialValues['player_id']);
			else
				$this->setPlayerId(0);

			if (isset($arrInitialValues['name']))
				$this->setName($arrInitialValues['name']);
			else
				$this->setName('');

			if (isset($arrInitialValues['surname']))
				$this->setSurname($arrInitialValues['surname']);
			else
				$this->setSurname('');

			if (isset($arrInitialValues['country_id']))
				$this->setCountryId($arrInitialValues['country_id']);
			else
				$this->setCountryId(0);

			if (isset($arrInitialValues['birthdate']))
				$this->setBirthdate($arrInitialValues['birthdate']);
			else
				$this->setBirthdate('');

			if (isset($arrInitialValues['points']))
				$this->setPoints($arrInitialValues['points']);
			else
				$this->setPoints(0);

			if (isset($arrInitialValues['ranking']))
				$this->setRanking($arrInitialValues['ranking']);
			else
				$this->setRanking(0);

			if (isset($arrInitialValues['sex']))
				$this->setSex($arrInitialValues['sex']);
			else
				$this->setSex('M');
				
			if (isset($arrInitialValues['active']))
				$this->setActive($arrInitialValues['active']);
			else
				$this->setActive(0);
		}
		
		public function setPlayerId($value){
			$this->_PlayerId = $value;
		}
		
		public function getPlayerId(){
			return $this->_PlayerId;
		}
		
		public function setName($value){
			$this->_Name = $value;
		}
		
		public function getName(){
			return $this->_Name;
		}
		
		public function setSurname($value){
			$this->_Surname = $value;
		}
		
		public function getSurname(){
			return $this->_Surname;
		}
		
		public function setCountryId($value){
			$this->_CountryId = $value;
		}
		
		public function getCountryId(){
			return $this->_CountryId;
		}
		
		public function setBirthdate($value){
			$this->_Birthdate = $value;
		}
		
		public function getBirthdate(){
			return $this->_Birthdate;
		}

		public function setPoints($value){
			$this->_Points = $value;
		}
		
		public function getPoints(){
			return $this->_Points;
		}

		public function setRanking($value){
			$this->_Ranking = $value;
		}

		public function getRanking(){
			return $this->_Ranking;
		}

		public function setActive($value){
			$this->_Active = $value;
		}
		
		public function getActive(){
			return $this->_Active;
		}
		
		public static function getAll(){
			$query = '
				SELECT players.player_id, players.sex, players.name, players.surname, countries.name as country_id, players.birthdate, 
				players.points, players.ranking, players.active
				FROM players
				INNER JOIN countries 
					ON players.country_id = countries.country_id
				ORDER BY players.ranking ASC';

			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
		
		public static function getAllOrderderByName(){
			$query = '
				SELECT players.player_id, players.sex, players.name, players.surname, countries.name as country_id, players.birthdate, 
				players.points, players.ranking, players.active
				FROM players
				INNER JOIN countries 
					ON players.country_id = countries.country_id
				ORDER BY players.name ASC, players.surname ASC';

			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
		
		public static function getAllOfSex($Sex, $active = true){
			$query = '
				SELECT players.player_id, players.sex, players.name, players.surname, countries.name as country_id, players.birthdate, 
				players.points, players.ranking, players.active
				FROM players
				LEFT JOIN countries 
					ON players.country_id = countries.country_id
				WHERE 1 = 1';
				
				if ($active)
					$query.= ' AND players.active = 1 ';
				
				$query .= '
					AND players.sex = "' . $Sex . '"
				ORDER BY players.name ASC';

			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
		
		public function getPlayer(){
			$query = '
				SELECT *
				FROM players
				WHERE player_id = ' . $this->_PlayerId;
			$dbHelper = new DBHelper($query);

			return $dbHelper->getResult();
		}

		public function update(){
			$query = '
				UPDATE players
				SET name = "' . $this->_Name . '", 
				surname = "' . $this->_Surname . '", 
				country_id = "' . $this->_CountryId . '", 
				birthdate = "' . $this->_Birthdate . '", 
				points = "' . $this->_Points . '", 
				ranking = "' . $this->_Ranking . '",
				sex = "' . $this->_Sex . '", 
				active = ' . $this->_Active . ' 
				WHERE player_id = ' . $this->_PlayerId;

			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();
		}

		private function changeStatus(){
			$query='
				UPDATE players
				SET active = ' . $this->getActive() . '
				WHERE player_id= ' . $this->getPlayerId();
			$dbHelper = new DBHelper($query);
			return ($dbHelper->getNumRows() > 0);
		}
		
		public function activate(){
			$this->setActive(1);
			return $this->changeStatus();
		}
		
		public function delete(){
			$this->setActive(0);
			return $this->changeStatus();
		}
		
		public function create(){
			$query = '
				INSERT INTO players
				(name, surname, country_id, birthdate, points, ranking, sex, active)
				VALUES
				("' . $this->getName() . '", "' . $this->getSurname() . '", ' . $this->getCountryId() . ', "' . $this->getBirthdate() . '", ' . $this->getPoints() . ', ' . $this->getRanking() . ', "' . $this->getSex() . '", ' . $this->getActive() . ')';

			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();
		}
		
		public function getPlayersWithNation(){
			$query = '
				SELECT * 
				FROM players
				WHERE country_id = ' . $this->getCountryId() . '
					AND active = 1';
			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}

		public function getPlayersWithNationAndSex(){
			$query = '
				SELECT * 
				FROM players
				WHERE country_id = ' . $this->getCountryId() . '
					AND sex = "' . $this->getSex() . '"
					AND active = 1';

			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}
		
	}
?>
